/*
 * Copyright (C) 2024-2025 Deepin Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "securitylevelitem.h"
#include "widgets/selectediconwidget.h"

//#include <DBackgroundGroup>
//#include <DApplicationHelper>
//#include <DFontSizeManager>

#include <QPainter>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGraphicsOpacityEffect>
#include <QDebug>

SecurityLevelItem::SecurityLevelItem(QWidget *parent)
    : HFrame1(parent)
    , m_icon(nullptr)
    , m_levelLabel(nullptr)
    , m_tipLabel(nullptr)
    , m_tipWarningLabel(nullptr)
    , m_selectedIconWidget(nullptr)
    , m_canClick(true)
{
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->setContentsMargins(0, 0, 0, 0);

    setLayout(mainLayout);
    // 设置布局大小改变策略
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    m_firstLayerWidgt = new QWidget(this);
    mainLayout->addWidget(m_firstLayerWidgt);

    QHBoxLayout *firstLayerLayout = new QHBoxLayout;
    firstLayerLayout->setContentsMargins(10, 0, 10, 0);
    m_firstLayerWidgt->setLayout(firstLayerLayout);

    m_icon = new QLabel(this);
    firstLayerLayout->addWidget(m_icon);

    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->setContentsMargins(5, 9, 0, 9);
    firstLayerLayout->addLayout(rightLayout, Qt::AlignLeft);

    m_levelLabel = new QLabel(this);
    m_levelLabel->setAlignment(Qt::AlignLeft);
    m_levelLabel->setWordWrap(true);
    m_levelLabel->setContentsMargins(0, 0, 0, 0);
    QFont font = m_levelLabel->font();
    font.setBold(true);
    m_levelLabel->setFont(font);
    rightLayout->addWidget(m_levelLabel, 0, Qt::AlignLeft);
    rightLayout->setSpacing(0);

    m_tipLabel = new QLabel("", this);
    m_tipLabel->setAlignment(Qt::AlignLeft);
    m_tipLabel->setWordWrap(true);
    m_tipLabel->adjustSize();
    rightLayout->addWidget(m_tipLabel);

    m_tipWarningLabel = new QLabel("", this);
    m_tipWarningLabel->setAlignment(Qt::AlignLeft);
//    m_tipWarningLabel->setElideMode(Qt::ElideMiddle);
//    DFontSizeManager::instance()->bind(m_tipWarningLabel, DFontSizeManager::SizeType::T8);
    m_tipWarningLabel->adjustSize();
    m_tipWarningLabel->setVisible(false);
    rightLayout->addWidget(m_tipWarningLabel);

    // 初始化是否选中图标标签
    m_selectedIconWidget = new def::widgets::SelectedIconWidget(this);
    m_selectedIconWidget->setFixedSize(16, 16);
    mainLayout->addWidget(m_selectedIconWidget, 0, Qt::AlignmentFlag::AlignRight);

    setBackgroundType(HFrame1::ItemBackground);
    // 设置边框的宽度
    setLineWidth(0);
}

SecurityLevelItem::~SecurityLevelItem()
{
}

// 设置相关标签名称
void SecurityLevelItem::setAccessibleParentText(QString sAccessibleName)
{
    m_firstLayerWidgt->setAccessibleName(sAccessibleName + "_backGroundWidget");
    m_levelLabel->setAccessibleName("backGroundWidget_levelLable");
    m_tipLabel->setAccessibleName("backGroundWidget_tipLable");
    m_selectedIconWidget->setAccessibleName(sAccessibleName + "_selectIconWidget");
}

// 设置文案是否换行
void SecurityLevelItem::setTipLabelTextWordWrap(bool bl)
{
    m_tipLabel->setWordWrap(bl);
}

// 设置文案省略显示模式
void SecurityLevelItem::setTipLabelTextElideMode(Qt::TextElideMode elideMode)
{
//    m_tipLabel->setElideMode(elideMode);
}

void SecurityLevelItem::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);

    if (m_canClick) {
        Q_EMIT clicked();
    }
}

void SecurityLevelItem::setIcon(const QIcon &icon)
{
    m_icon->setPixmap(icon.pixmap(16, 20));
}

void SecurityLevelItem::setLevelText(const QString &text)
{
    m_levelLabel->setText(text);
}

void SecurityLevelItem::setTipText(const QString &text)
{
    m_tipLabel->setText(text);
}

// 设置警告提示文字
void SecurityLevelItem::setWarningText(const QString &text)
{
    m_tipWarningLabel->setVisible(true);
    QPalette pal;
    pal.setColor(QPalette::Text, QColor("#FF5D00"));
    m_tipWarningLabel->setPalette(pal);
    m_tipWarningLabel->setText(text);
}

void SecurityLevelItem::setSelected(const bool &selected)
{
    QPixmap selectImg;
    qreal opacity;
    // 根据状态设置属性
    if (selected) {
        opacity = 1;
    } else {
        opacity = 0.5;
    }
    QGraphicsOpacityEffect *opacityEffect[4];
    for (int i = 0; i < 4; i++) {
        opacityEffect[i] = new QGraphicsOpacityEffect(this);
        opacityEffect[i]->setOpacity(opacity);
    }
    // 设置透明度
    m_icon->setGraphicsEffect(opacityEffect[0]);
    m_levelLabel->setGraphicsEffect(opacityEffect[1]);
    m_tipLabel->setGraphicsEffect(opacityEffect[2]);
    m_tipWarningLabel->setGraphicsEffect(opacityEffect[3]);
    // 设置是否选中
    m_selectedIconWidget->setVisible(selected);
}

void SecurityLevelItem::setEnable(const bool &enable)
{
    qreal opacity = 1;
    if (enable) {
        opacity = 1;
        m_canClick = true;
    } else {
        opacity = 0.5;
        m_canClick = false;
    }
    QGraphicsOpacityEffect *opacityEffect[5];
    for (int i = 0; i < 5; i++) {
        opacityEffect[i] = new QGraphicsOpacityEffect(this);
        opacityEffect[i]->setOpacity(opacity);
    }

    m_icon->setGraphicsEffect(opacityEffect[0]);
    m_levelLabel->setGraphicsEffect(opacityEffect[1]);
    m_tipLabel->setGraphicsEffect(opacityEffect[2]);
    m_tipWarningLabel->setGraphicsEffect(opacityEffect[3]);
    m_selectedIconWidget->setGraphicsEffect(opacityEffect[4]);
}
